CONTENTS | INDEX | PREV | NEXT
strncat
NAME
strncat - concactenate a string to an existing string up to a
maximum number of characters
SYNOPSIS
char *d = strncat(d, s, n);
char *d;
const char *s;
int n;
FUNCTION
scans the destination buffer for the nul terminator and then
appends the source string to the destination buffer (removing
the nul terminator and placing one at the end after the
concactenation). However, only up to n characters is concactenated
including the nul. If the source string is exactly n characters
long no nul will be appended. If the source string is longer
than n characters then only the first n characters of the source
string will be appended (and no nul will be).
A pointer to the beginning of the destination buffer is returned.
EXAMPLE
#include <stdio.h>
#include <string.h>
#include <assert.h>
main()
{
char d[32];
char *s1 = "fu";
char *s2 = "bar";
char *p;
d[2] = 23;
d[5] = 24;
d[6] = 25;
strcpy(d, s1); /* overwrites d[2] with a nul */
assert(d[2] == 0);
p = strncat(d, s2, 3); /* does NOT overwrite d[5] with a nul */
assert(d[5] == 24);
assert(p == d);
strcpy(d, s1);
p = strncat(d, s2, 20); /* does */
assert(p == d);
assert(d[5] == 0);
assert(d[6] == 25); /* stops at the nul, so d[6] was not modified */
puts(d); /* fubar */
return(0);
}
INPUTS
char *d; pointer to destination buffer which already contains
a string (which could be just a 0).
char *s; pointer to the nul terminated source string
int n; maximum number of characters to concactenate
RESULTS
char *d; same as the first argument, a pointer to destination buffer.
SEE ALSO
strncpy, strcpy, strcat